package com.steven.im.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.google.zxing.integration.android.IntentIntegrator;
import com.steven.im.R;
import com.steven.im.base.Constant;
import com.steven.im.manager.BrightnessManager;
import com.steven.im.ui.activity.IMAddFriendsActivity;
import com.steven.im.ui.activity.IMGroupActivity;
import com.steven.im.ui.activity.IMScanActivity;

public class IMPopWindows implements View.OnClickListener {
    private Context context;
    private View view;
    private PopupWindow mPopupWindow;

    public IMPopWindows(Context context, View view) {
        mPopupWindow = new PopupWindow(context);
        this.context = context;
        this.view = view;
    }

    @SuppressLint("InflateParams")
    public void showPop() {
        BrightnessManager.lightoff(context);
        // 设置布局文件
        mPopupWindow.setContentView(LayoutInflater.from(context).inflate(R.layout.im_pop, null));
        // 为了避免部分机型不显示，我们需要重新设置一下宽高
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置pop透明效果
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x0000));
        // 设置pop出入动画
        mPopupWindow.setAnimationStyle(R.style.pop_add);
        // 设置pop获取焦点，如果为false点击返回按钮会退出当前Activity，如果pop中有Editor的话，focusable必须要为true
        mPopupWindow.setFocusable(true);
        // 设置pop可点击，为false点击事件无效，默认为true
        mPopupWindow.setTouchable(true);
        // 设置点击pop外侧消失，默认为false；在focusable为true时点击外侧始终消失
        mPopupWindow.setOutsideTouchable(true);
        // 相对于 + 号正下面，同时可以设置偏移量

        if (Build.VERSION.SDK_INT >= 24) {
            int[] point = new int[2];
            view.getLocationInWindow(point);
            mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, (point[0] + view.getWidth()), point[1] + view.getHeight() + 20);
        } else {
            mPopupWindow.showAsDropDown(view);
        }

        // 设置pop关闭监听，用于改变背景透明度
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //操作  //设置  背景变亮
                BrightnessManager.lighton(context);
            }
        });
        setClick();
    }

    private void setClick() {
        //添加好友
        LinearLayout addFriends = mPopupWindow.getContentView().findViewById(R.id.ll_addFriends);
        //发起群聊
        LinearLayout toGroupChat = mPopupWindow.getContentView().findViewById(R.id.ll_toGroupChat);
        //扫一扫
        LinearLayout scan = mPopupWindow.getContentView().findViewById(R.id.ll_scan);
        addFriends.setOnClickListener(this);
        toGroupChat.setOnClickListener(this);
        scan.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_addFriends:
                mPopupWindow.dismiss();
                context.startActivity(new Intent(context, IMAddFriendsActivity.class));
                break;
            case R.id.ll_toGroupChat:
                mPopupWindow.dismiss();
                context.startActivity(new Intent(context, IMGroupActivity.class));
                break;
            case R.id.ll_scan:
                mPopupWindow.dismiss();
                new IntentIntegrator((Activity) context)
                        // 扫码的类型,条形码或者二维码
                        .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
                        //这个参数可以设置也可以不设置，设置后使用自定义的二维码扫描框，不设置使用默认的
                        .setCaptureActivity(IMScanActivity.class)
                        .setRequestCode(Constant.QRCode)
                        // 设置提示语
                        .setPrompt("")
                        //设置使用那个摄像头
                        .setCameraId(0)
                        //是否在扫描完成后有提示音
                        .setBeepEnabled(true)
                        //扫完码之后生成二维码的图片
                        .setBarcodeImageEnabled(true)
                        //初始化扫描
                        .initiateScan();
                break;
        }
    }

}
